/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: ConstantFactory
 * Author:   zhouheng
 * Date:     2018/6/18 14:18
 * Description: 常量工厂类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.hengby.hengcrud.core.common.factory;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hengby.hengcrud.core.constants.cache.Cache;
import com.hengby.hengcrud.core.constants.cache.CacheKey;
import com.hengby.hengcrud.core.support.StrKit;
import com.hengby.hengcrud.core.util.Convert;
import com.hengby.hengcrud.core.util.SpringContextHold;
import com.hengby.hengcrud.core.util.ToolUtil;
import com.hengby.hengcrud.modular.sys.mapper.DeptMapper;
import com.hengby.hengcrud.modular.sys.mapper.DictMapper;
import com.hengby.hengcrud.modular.sys.mapper.RoleMapper;
import com.hengby.hengcrud.modular.sys.mapper.UserMapper;
import com.hengby.hengcrud.modular.sys.model.Dept;
import com.hengby.hengcrud.modular.sys.model.Dict;
import com.hengby.hengcrud.modular.sys.model.Role;
import com.hengby.hengcrud.modular.sys.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import javax.tools.Tool;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br> 
 * 〈常量工厂类〉
 *
 * @author zhouheng
 * @create 2018/6/18
 * @since 1.0.0
 */
@Component
@DependsOn(value = "springContextHold")
public class ConstantFactory implements IConstantFactory{

    public static DictMapper dictMapper = SpringContextHold.getBean(DictMapper.class);
    public static UserMapper userMapper = SpringContextHold.getBean(UserMapper.class);
    public static RoleMapper roleMapper = SpringContextHold.getBean(RoleMapper.class);
    public static DeptMapper deptMapper = SpringContextHold.getBean(DeptMapper.class);

    public static IConstantFactory me() {
        return SpringContextHold.getBean(IConstantFactory.class);
    }

    @Override
    public String getUserNameById(int id) {

        User user = userMapper.selectById(id);
        if (user != null){
            String userName = user.getName();
            return userName;
        }else {
            return "--";
        }
    }

    @Override
    public String getUserAccountById(int id) {


        return null;
    }

    @Override
    @Cacheable(cacheNames = Cache.CONSTANT, key = "'" + CacheKey.ROLES_NAME + "'+#roleIds")
    public String getRoleName(String roleIds) {

        Integer[] roleArr = Convert.toIntArray(roleIds);
        StringBuilder sb = new StringBuilder();

        for (Integer roleId : roleArr){
            Role roleObj = roleMapper.selectById(roleId);
            if (ToolUtil.isNotEmpty(roleObj) && ToolUtil.isNotEmpty(roleObj.getRoleName())) {
                sb.append(roleObj.getRoleName()).append(",");
            }
        }
        // 清除末尾符号
        return StrKit.removeSuffix(",",sb.toString());
    }

    @Override
    public String getSingleRoleName(Integer roleId) {
        if (0 == roleId) {
            return "--";
        }
        Role roleObj = roleMapper.selectById(roleId);
        if (ToolUtil.isNotEmpty(roleObj) && ToolUtil.isNotEmpty(roleObj.getRoleName())) {
            return roleObj.getRoleName();
        }
        return "";
    }

    @Override
    public String getSingleRoleTip(Integer roleId) {
        if (roleId == null){
            return "--";
        }else {
            Role role = roleMapper.selectById(roleId);
            if (ToolUtil.isNotEmpty(role) && ToolUtil.isNotEmpty(role.getRoleName())){
                return role.getTips();
            }
        }

        return null;
    }

    @Override
    @Cacheable(cacheNames = Cache.CONSTANT, key = "'" + CacheKey.DEPT_NAME + "'+#deptId")
    public String getDeptName(Integer deptId) {
        if (ToolUtil.isNotEmpty(deptId)){
            Dept dept = deptMapper.selectById(deptId);
            if (ToolUtil.isNotEmpty(dept) && ToolUtil.isNotEmpty(dept.getSimplename())){
                return dept.getSimplename();
            }
        }
        return "";
    }

    @Override
    public String getMenuNames(String menuIds) {
        return null;
    }

    @Override
    public String getMenuName(Long menuId) {
        return null;
    }

    @Override
    public String getMenuNameByCode(String code) {
        return null;
    }

    @Override
    public String getDictName(Integer dictId) {
        return null;
    }

    @Override
    public String getSexName(Integer sex) {

        return getDictsByName("性别", sex);
    }

    @Override
    public String getStatusName(Integer status) {
        return null;
    }

    @Override
    public String getMenuStatusName(Integer status) {
        return null;
    }

    @Override
    public String getCacheObject(String para) {
        return null;
    }

    @Override
    public List<Integer> getSubDeptId(Integer deptid) {

        Wrapper<Dept> wrapper = new EntityWrapper<>();
        wrapper.like("pids","%["+deptid+"]%");

        List<Dept> depts = deptMapper.selectList(wrapper);

        List<Integer> list = new ArrayList<>();

        if (ToolUtil.isEmpty(deptid) && ToolUtil.length(depts) > 0){
            for (int i = 0; i < depts.size(); i++){
                list.add(depts.get(i).getId());
            }
        }

        return list;
    }

    @Override
    public List<Integer> getParentDeptIds(Integer deptid) {
        return null;
    }

    @Override
    public List<Dict> findInDict(Integer id) {
        if (ToolUtil.isEmpty(id)) {
            return null;
        } else {
            EntityWrapper<Dict> wrapper = new EntityWrapper<>();
            List<Dict> dicts = dictMapper.selectList(wrapper.eq("pid", id));
            if (dicts == null || dicts.size() == 0) {
                return null;
            } else {
                return dicts;
            }
        }
    }

    /**
     * 根据字典字段名称和值获取对应的中文值
     */
    private String getDictsByName(String name,Integer id){
        Dict temp = new Dict();
        temp.setName(name);
        Dict dict = dictMapper.selectOne(temp);
        if (dict == null){
            return "";
        }else{
            Wrapper<Dict> wrapper = new EntityWrapper<>();
            wrapper = wrapper.eq("pid", dict.getId());
            List<Dict> dicts = dictMapper.selectList(wrapper);
            for (Dict item : dicts) {
                if (item.getNum() != null && item.getNum().equals(id)) {
                    return item.getName();
                }
            }
            return "";
        }
    }
}